﻿using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using DataModel;
using System.Data.Services.Providers;

namespace SocialPhotos
{
    public class PhotoCloud : DataService<DataModel.SocialPhotoModelContainer>, IServiceProvider
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            // Examples:
            //config.SetEntitySetAccessRule("Categories", EntitySetRights.All);
            //config.SetEntitySetAccessRule("Tags", EntitySetRights.All);
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
     
        }
        public object GetService(Type serviceType)
        {
            if (serviceType == typeof(IDataServiceStreamProvider))
            {
                // Return the stream provider to the data service.
                return new PhotoServiceStreamProvider(this.CurrentDataSource);
            }
            return null;
        }

        //  [WebInvoke(Method = "POST")]

        #region Custom Webmethod
        [WebGet]
        public void like(int id)
        {
            try
            {
                SocialPhotoModelContainer context = this.CurrentDataSource;
                Photo photo = context.Photos.Single(p => p.Id == id);
                if (photo != null)
                {
                    photo.Likes++;
                    context.SaveChanges();
                }
            }
            catch (Exception)
            {
            }
        }

        [WebGet]
        public void dislike(int id)
        {
            try
            {
                SocialPhotoModelContainer context = this.CurrentDataSource;
                Photo photo = context.Photos.Single(p => p.Id == id);
                if (photo != null)
                {
                    photo.Dislikes++;
                    context.SaveChanges();
                }
            }
            catch (Exception)
            {
            }
        }

        [WebGet]
        public IQueryable<Photo> getTodayPhotos()
        {
            SocialPhotoModelContainer context = this.CurrentDataSource;
            return context.Photos.Take(32);
        }

        [WebGet]
        public IQueryable<Photo> getTopRated(int number)
        {
            try
            {
                var photo = (from d in this.CurrentDataSource.Photos
                             orderby d.Likes descending
                            select d).Take(number);
                return photo;
            }
            catch
            {
                return null;
            }
        }

        [WebGet]
        public IQueryable<Photo> getRandomPhotos(int number)
        {
            var qry = from photo in this.CurrentDataSource.Photos
                    where photo.PublicLevel == 0
                    orderby Guid.NewGuid()
                    select photo;
            return qry.Take(number);
        }

        [WebGet]
        public IQueryable<Photo> getPhotosByCategoryId(int id)
        {
            try
            {
                var photo = from d in this.CurrentDataSource.Photos
                            where d.Category.FirstOrDefault().Id == id
                            select d;
                return photo;
            }
            catch
            {
                return null;
            }
        }

        [WebGet]
        public IQueryable<Photo> getPhotosByUserId(int id)
        {
            var photo = from d in this.CurrentDataSource.Photos
                        where d.User.Id == id
                        select d;
            return photo;
        }

        //[WebGet]
        //public void getNearByPhotos(float longitude, float latitude, float radius)
        //{
        //    return null;
        //}

        [WebInvoke(Method = "POST")]
        public void postComment(int userID, int photoId, string comment)
        {
            SocialPhotoModelContainer context = this.CurrentDataSource;
            Photo photo = context.Photos.Single(p => p.Id == photoId);
            User user = context.Users.Single(p => p.Id == userID);

            Comment pComment = new Comment{User = user, Photo = photo, Content = comment, PostedTime = System.DateTime.Now};
            context.AddToComments(pComment);
            context.SaveChanges();
        }

        [WebGet]
        public IQueryable<Comment> getCommentsOfAPhoto(int photoId)
        {
            var comments = from c in this.CurrentDataSource.Comments
                           where c.Photo.Id == photoId
                           select c;
            return comments;
        }

        //[WebGet]
        //public IQueryable<Object> getCommentsAndPhoto(int photoId)
        //{
        //    SocialPhotoModelContainer context = this.CurrentDataSource;
        //    var comments = from p in this.CurrentDataSource.Photos
        //                   join comment in this.CurrentDataSource.Comments on p.Id equals comment.Photo.Id
        //                   select new { p.FileName, comment.Content };
        //    return comments;
        //}
        #endregion
    }
}
